package com.springboot.utils.myFile;

import com.springboot.utils.myLog.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import static com.springboot.config.PathConf.recordFilePath;

/**
 * @author : sugar without coffee
 * @version 1.0.0
 */
public class FileUtils {

    /**
     * 上传MultipartFile文件
     * 成功：返回文件名,文件名为文件内容的hash值
     * 失败：返回error
     * */
    public static String uploadFile(MultipartFile mFile) {
        try {
            File file = File.createTempFile("temp", null);
            //sourceFile为传入的MultipartFile
            mFile.transferTo(file);
            InputStream inputStream = new FileInputStream(file);
            file.deleteOnExit();
            String fileName[] = mFile.getOriginalFilename().split("\\.");
            String uploadName = MD5Util.getFileMD5HashCode32ByInputStream(inputStream)+ "." +fileName[1];
            FileUtils.upload(mFile.getBytes(), recordFilePath,uploadName);
            Slf4j.logger.info("获取到文件："+uploadName);
            return uploadName;
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    /**
     * @param file 文件
     * @param filePath 文件存放路径
     * @param fileName 源文件名
     * @return
     */
    public static void upload(byte[] file, String filePath, String fileName) throws Exception {
        File targetFile = new File(filePath);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        FileOutputStream out = new FileOutputStream(filePath + fileName);
        out.write(file);
        out.flush();
        out.close();
    }

    public static synchronized String createtFileName() {
        Date dt = new Date(System.currentTimeMillis());
        SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String fileName= fmt.format(dt);
        return fileName;
    }
}
